package com.httpClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

public class HttpTest {
//	public static void main(String[] args) {
//		MyHttpClient  my=new MyHttpClient(); //应该先调登陆接口
//		my.initClient();
//		String url="http://localhost:8080/mavenDemo-SpringMvc/http/http";
//		String loginUrl="http://localhost:8080/mavenDemo-SpringMvc/login/phoneLogin";
//		try {
//			List<NameValuePair> params=new ArrayList<NameValuePair>();
//			params.add(new BasicNameValuePair("userName","admin"));
//			params.add(new BasicNameValuePair("password","admin"));
//			String login=my.postData(loginUrl,params);
//			String ss = my.postData(url);
//			System.out.println(ss);
//		} catch (DajieHttpRequestException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		
//		
//	}
	
//	public static void main(String[] args) {
//		int MAX_POINT=300;
//		int size=3609;
//		if (size > MAX_POINT){
//			int total = -1; // 定位点的总数
//			int multiple = -1; // 定位点是MAX_POINT的多少倍
//			int remainder = -1; // 定位点除以MAX_POINT的到的余数
//			int surplus = -1; // 最后需要剔除的余数的个数
//
//			total = size; // 3609
//			multiple = total/MAX_POINT; // 12
//			remainder = total - (multiple * MAX_POINT); // 9
//			surplus = remainder/multiple; // 0
//			if (multiple >= 1){
//				int numb = -1;
//				if (surplus != 0){
//					numb = total / multiple / surplus;
//				}
//
//				for (int i = 1, j = 0, k = 0; i < total - 1 && multiple > 1; i++) {
//					if (j == multiple - 1){
//						j = 0;
//						if (surplus > 0){
//							if (numb != -1 && numb - 1 == k){
//								mArrayList.get(i).setDrivingplanid(-1); // 将DrivingPlanID设置为-1
//								k = 0;
//							} else {
//								k++;
//							}
//						}
//
//					} else {
//						mArrayList.get(i).setDrivingplanid(-1); // 将DrivingPlanID设置为-1
//						j++;
//					}
//				}
//
//				if (multiple == 1 && surplus > 0){
//					int delete = 0;
//					for (int i = 1, k = 0; i < total - 1; i++) {
//						if (mArrayList.get(i).getDrivingplanid() != -1){
//							if (numb - 1 == k){
//								mArrayList.get(i).setDrivingplanid(-1);
//								delete++;
//								k = 0;
//							} else {
//								k++;
//							}
//
//						}
//
//						if (delete == surplus){
//							break;
//						}
//
//					}
//				}
//
//			}
//
//		}
//	}
	/**
	 * 与TX连接
	 *
	 * @return
	 */
	public String connectionTX(String  sendMsg) throws Exception {
		String urlstr = "";
		// 开启HTTP连接
		URL url = new URL(urlstr);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

		// 设置HTTP请求相关信息
		httpConn.setRequestProperty("Content-Length", String.valueOf(sendMsg.getBytes("UTF-8").length));
		httpConn.setRequestProperty("Content-Type", "application/json");
		httpConn.setRequestMethod("POST");

		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);

		// 进行HTTP请求
		OutputStream outObject = httpConn.getOutputStream();
		outObject.write(sendMsg.getBytes("UTF-8"));

		// 关闭输出流
		outObject.close();

		// 获取HTTP响应数据
		InputStreamReader isr = new InputStreamReader(httpConn.getInputStream(), "utf-8");
		BufferedReader inReader = new BufferedReader(isr);
		StringBuffer result = new StringBuffer();
		String inputLine;
		while ((inputLine = inReader.readLine()) != null) {
			result.append(inputLine);
		}

		// 打印HTTP响应数据
		//  System.out.println(result);

		// 关闭输入流
		inReader.close();
		isr.close();
		return result.toString();

	}
	
}
